package com.zhz.system.utils;

import com.zhz.common.shiro.entity.UserProfileResponse;
import com.zhz.model.system.contract.dto.*;
import com.zhz.model.system.contract.param.PermissionQueryParam;
import com.zhz.model.system.contract.param.RolePermissionQueryParam;
import com.zhz.model.system.contract.param.RoleQueryParam;
import com.zhz.model.system.contract.param.UserRoleQueryParam;
import com.zhz.system.contract.enums.PermissionTypeEnum;
import com.zhz.system.repository.service.PermissionService;
import com.zhz.system.repository.service.RolePermissionService;
import com.zhz.system.repository.service.RoleService;
import com.zhz.system.repository.service.UserRoleService;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zhouhengzhe
 * @date 2023/5/28
 */
public class UserProfileResponseUtil {

    /**
     * 构建权限相关信息
     *
     * @param userProfileResponse   用户信息
     * @param userRoleService       用户角色服务
     * @param userDTO               用户信息
     * @param roleService           角色服务
     * @param rolePermissionService 角色权限服务
     * @param permissionService     权限服务
     * @return 用户信息
     */
    public static UserProfileResponse build(UserProfileResponse userProfileResponse,
                                            UserRoleService userRoleService,
                                            UserDTO userDTO,
                                            RoleService roleService,
                                            RolePermissionService rolePermissionService,
                                            PermissionService permissionService) {
        //获取用户角色信息
        List<UserRoleDTO> list = userRoleService.list(
                UserRoleQueryParam.builder().userId(userDTO.getId()).build()
        );
        //为空直接返回
        if (CollectionUtils.isEmpty(list)) {
            return userProfileResponse;
        }
        //获取角色信息
        List<RoleDTO> roleDTOS = roleService.list(
                RoleQueryParam.builder().ids(list.stream().map(UserRoleDTO::getRoleId).collect(Collectors.toSet())).build()
        );
        //角色信息为空，
        if (CollectionUtils.isEmpty(roleDTOS)) {
            return userProfileResponse;
        }
        //获取角色权限信息
        List<RolePermissionDTO> rolePermissionDTOS = rolePermissionService.list(
                RolePermissionQueryParam.builder().roleIdSet(
                        roleDTOS.stream().map(RoleDTO::getId).collect(Collectors.toSet())
                ).build()
        );
        if (CollectionUtils.isEmpty(rolePermissionDTOS)) {
            return userProfileResponse;
        }
        List<PermissionDTO> permissionDTOS = permissionService.list(
                PermissionQueryParam.builder().idSet(
                        rolePermissionDTOS.stream().map(RolePermissionDTO::getPermissionId).collect(Collectors.toSet())
                ).build());
        if (CollectionUtils.isEmpty(permissionDTOS)) {
            return userProfileResponse;
        }
        //获取可访问的api权限
        Set<String> apis = permissionDTOS.stream()
                .filter(permissionDTO -> PermissionTypeEnum.PERMISSION_API.getCode().equals(permissionDTO.getType()))
                .map(PermissionDTO::getCode)
                .collect(Collectors.toSet());
        //获取可访问的权限点权限
        Set<String> points = permissionDTOS.stream()
                .filter(permissionDTO -> PermissionTypeEnum.PERMISSION_POINT.getCode().equals(permissionDTO.getType()))
                .map(PermissionDTO::getCode)
                .collect(Collectors.toSet());
        //获取可访问的菜单权限
        Set<String> menus = permissionDTOS.stream()
                .filter(permissionDTO -> PermissionTypeEnum.PERMISSION_MENU.getCode().equals(permissionDTO.getType()))
                .map(PermissionDTO::getCode)
                .collect(Collectors.toSet());
        userProfileResponse.setRoles(UserProfileResponse.RoleInfo
                .builder()
                .apis(apis)
                .points(points)
                .menus(menus)
                .build());
        return userProfileResponse;
    }

    /**
     * 构建权限相关信息
     *
     * @param userProfileResponse 用户信息
     * @param permissionDtoList   权限信息集合
     * @return 用户信息
     */
    public static UserProfileResponse build(UserProfileResponse userProfileResponse,
                                            List<PermissionDTO> permissionDtoList) {
        if (CollectionUtils.isEmpty(permissionDtoList)) {
            return userProfileResponse;
        }
        //获取可访问的api权限
        Set<String> apis = permissionDtoList.stream()
                .filter(permissionDTO -> PermissionTypeEnum.PERMISSION_API.getCode().equals(permissionDTO.getType()))
                .map(PermissionDTO::getCode)
                .collect(Collectors.toSet());
        //获取可访问的权限点权限
        Set<String> points = permissionDtoList.stream()
                .filter(permissionDTO -> PermissionTypeEnum.PERMISSION_POINT.getCode().equals(permissionDTO.getType()))
                .map(PermissionDTO::getCode)
                .collect(Collectors.toSet());
        //获取可访问的菜单权限
        Set<String> menus = permissionDtoList.stream()
                .filter(permissionDTO -> PermissionTypeEnum.PERMISSION_MENU.getCode().equals(permissionDTO.getType()))
                .map(PermissionDTO::getCode)
                .collect(Collectors.toSet());
        userProfileResponse.setRoles(UserProfileResponse.RoleInfo
                .builder()
                .apis(apis)
                .points(points)
                .menus(menus)
                .build());
        return userProfileResponse;
    }

}
